package com.jbm.jbmsupplier.zp.presenters;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.zp.FactouryActivity.FacturyChatBaseAdapter.ChatBaseAdapter;
import com.jbm.jbmsupplier.zp.FactouryActivity.FacturyChatBaseAdapter.ChatBaseInterfaces;
import com.jbm.jbmsupplier.zp.adapters.ChatMsgViewAdapter;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.models.ChatBaseInterface;
import com.jbm.jbmsupplier.zp.utils.DataFormate;
import com.jbm.jbmsupplier.zp.views.ChatBaseViews;

import java.util.List;

/**
 * Created by 家帮忙 on 2017/7/17.
 */

public class ChatBaseActivitys implements ChatBaseInterface{
    private Context context;
    private ChatBaseViews chatBaseViews;
    private View view;
    private String stShowSendTime;
    private int curTime,upcurTime;
    private ChatBaseAdapter chatbaseadapter;
    private List<ChatMsgEntity> coll ;
    private int to_id;
    private MediaPlayer mMediaPlayer;
    private AnimationDrawable animationDrawable;
    public ChatBaseActivitys(Context context, ChatBaseViews chatBaseViews) {
        this.context = context;
        this.chatBaseViews = chatBaseViews;
    }


    @Override
    public void getToid(int to_id) {
        this.to_id=to_id;
    }

    @Override
    public View setLayout(int tag) {
       switch (tag){
           case 7:
             view=  View.inflate(context,R.layout.chatting_item_msg_text_right,null);
           break;
           case 8:
               view=  View.inflate(context,R.layout.chatting_item_msg_text_left,null);
               break;
           case 9:
               view=  View.inflate(context,R.layout.chatting_item_msg_picture_right,null);
               break;
           case 10:
               view=  View.inflate(context,R.layout.chatting_item_msg_picture_left,null);
               break;
           case 11:
               view=  View.inflate(context,R.layout.chatting_item_msg_voice_right,null);
               break;
           case 12:
               view=  View.inflate(context,R.layout.chatting_item_msg_voice_left,null);
               break;
           case 13:
               view=  View.inflate(context,R.layout.chatting_item_msg_pay,null);
               break;
           case 14:
               view=  View.inflate(context,R.layout.chatting_item_msg_pay,null);
               break;
       }
        return view;
    }

    @Override
    public void initData(final  ChatMsgViewAdapter.ViewHolder viewHolder, int position, int type, final List<ChatMsgEntity> coll) {
                     this.coll=coll;
                  if (chatbaseadapter==null){
                      chatbaseadapter=new ChatBaseAdapter();
                  }
        switch (type){
            case 7://发送文本
                setTimedate(coll.get(position),position,viewHolder.tvSendTime,coll);
                ChatBaseInterfaces sendtext = chatbaseadapter.setTag(7);
                sendtext.setDataAdapter(viewHolder,position,type,coll, stShowSendTime,context,to_id);
                setImageOnClick(viewHolder.image_shibai,"文本",viewHolder.tvContent.getText().toString(),coll.get(position),viewHolder);//发送失败的回掉
                break;
            case 8://接收文本
                setTimedate(coll.get(position),position,viewHolder.tvSendTime,coll);
                ChatBaseInterfaces recesendtext = chatbaseadapter.setTag(8);
                recesendtext.setDataAdapter(viewHolder,position,type,coll, stShowSendTime,context,to_id);
                break;
            case 9://发送图片
                setTimedate(coll.get(position),position,viewHolder.tvSendTime,coll);
                ChatBaseInterfaces endtextpic = chatbaseadapter.setTag(9);
                endtextpic.setDataAdapter(viewHolder,position,type,coll, stShowSendTime,context,to_id);
                setImageOnClick(viewHolder.image_shibai,"图片",coll.get(Integer.parseInt(viewHolder.imtv_chatcontent.getTag()+"")).getRoute(),coll.get(position),viewHolder);//发送失败的回掉
                break;
            case 10://接收图片
                setTimedate(coll.get(position),position,viewHolder.tvSendTime,coll);
                ChatBaseInterfaces recesendpic = chatbaseadapter.setTag(10);
                recesendpic.setDataAdapter(viewHolder,position,type,coll, stShowSendTime,context,to_id);
                break;

            case 11://发送语音
                setTimedate(coll.get(position),position,viewHolder.tvSendTime,coll);
                ChatBaseInterfaces endtextaudi = chatbaseadapter.setTag(11);
                endtextaudi.setDataAdapter(viewHolder,position,type,coll, stShowSendTime,context,to_id);
                viewHolder.vodiu_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        playMusic(coll.get(Integer.parseInt(viewHolder.av_userhead.getTag()+"")).getMediaName(),viewHolder.av_userhead,true);
                    }
                });
                				setImageOnClick(viewHolder.image_shibai,"语音",coll.get(Integer.parseInt(viewHolder.av_userhead.getTag()+"")).getMediaName(),coll.get(position),viewHolder);

                break;
            case 12://接收语音
                setTimedate(coll.get(position),position,viewHolder.tvSendTime,coll);
                ChatBaseInterfaces receendtextaudi = chatbaseadapter.setTag(12);
                receendtextaudi.setDataAdapter(viewHolder,position,type,coll, stShowSendTime,context,to_id);
                viewHolder.vodiu_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        playMusic(coll.get(Integer.parseInt(viewHolder.av_userhead.getTag()+"")).getMediaName(),viewHolder.av_userhead,false);
                    }
                });
                break;

            case 13:

                break;
            case 14:
                setTimedate(coll.get(position),position,viewHolder.tvSendTime,coll);
//				viewHolder.tvSendTime.setText(stShowSendTime);
//				viewHolder.tv_paydate.setText(coll.get(position).getPayDate());//付款时间
//              viewHolder.tv_pay_money.setText(coll.get(position).getPayMoney());//支付金额
//				viewHolder.tv_receivables.setText(OHInterface.getG_server().nickname);//收款方
//				viewHolder.tv_receivables_mode.setText(coll.get(position).getPayMode());//支付类型
//				viewHolder.tv_paystate.setText(coll.get(position).getPayState());//状态
                break;

        }





    }
    public void setImageOnClick(final ImageView image, final String tag, final String name, final ChatMsgEntity col,final ChatMsgViewAdapter.ViewHolder viewHolder){
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.image_shibai.setVisibility(View.GONE);
           if (col.getTags()==1){
               chatBaseViews.setUpError(tag,name,col.getDate(),col.getCgid(),col.getTags());
           }else{
                chatBaseViews.setUpError(tag,name,col.getDate(),-1,-1);}
            }
        });

        if (coll.get(Integer.parseInt(image.getTag() + "")).getIsboolean()==null){

        }else if (coll.get(Integer.parseInt(image.getTag() + "")).getIsboolean().equals("false")){
            viewHolder.image_shibai.setVisibility(View.VISIBLE);

        }else if (coll.get(Integer.parseInt(image.getTag() + "")).getIsboolean().equals("true")){
            viewHolder.image_shibai.setVisibility(View.GONE);
        }



    }

    /**
     * 设置消息时间
     */
    public void setTimedate(ChatMsgEntity positions,int position,TextView tvSendTime,List<ChatMsgEntity> coll){
        if (position - 1<0 ){
            try {
                stShowSendTime = DataFormate.getDateTimeFromMillisecond(DataFormate.StringT,DataFormate.stringToLong(positions.getDate(),DataFormate.StringF));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            try {
                curTime = Integer.parseInt(DataFormate.getDateTimeFromMillisecond(DataFormate.Stringmm, DataFormate.stringToLong(positions.getDate(), DataFormate.StringF)));
                upcurTime = Integer.parseInt(DataFormate.getDateTimeFromMillisecond(DataFormate.Stringmm, DataFormate.stringToLong(coll.get(position - 1).getDate(), DataFormate.StringF)));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (curTime - upcurTime == 0) {
                tvSendTime.setVisibility(View.GONE);
            } else {
                tvSendTime.setVisibility(View.VISIBLE);
            }
            stShowSendTime = DataFormate.getShowTime(coll.get(position).getDate());
        }
    }

    /**
     * @Description
     * @param name
     * 播放语音
     */
    private void playMusic(String name, final ImageView imageView, final boolean bool) {
        try {
            mMediaPlayer = new MediaPlayer();

            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
            }
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(name);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            starAnimation(imageView,bool);
            chatBaseViews.setClckMedia(mMediaPlayer);
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {

                    stopAnimation();
                    if (bool==true){
                        imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.vcs));
                    }else {
                        imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.vvvv));
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //播放动画
    public void starAnimation(ImageView imageView,boolean bool){
        if (bool==true){
            imageView.setImageResource(R.drawable.animations);
        }else if (bool==false){
            imageView.setImageResource(R.drawable.animation);}
        animationDrawable = (AnimationDrawable) imageView.getDrawable();
        animationDrawable.start();
    }
    //停止动画
    public void stopAnimation(){
        if (animationDrawable!=null){
            animationDrawable.stop();
        }
    }
}
